<?php
namespace common\models;

use Yii;
use yii\base\Model;

/**
 * Login form
 */
class AuthItemForm extends Model
{

    public $name;

    public $type;

    public $description;

    public $isNewRecord;

    public $child;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'name',
                    'type',
                    'description'
                ],
                'required'
            ],
            [
                'name',
                'unique',
                'targetClass' => 'common\models\AuthItem',
                'message' => '此名称已经被占用。'
            ],
            [
                'type',
                'integer'
            ],
            [
                [
                    'name',
                    'description'
                ],
                'string',
                'max' => 25
            ]
        ];
    }

    public function attributeLabels()
    {
        return [
            'name' => '名称',
            'type' => '类型',
            'description' => '描述',
            'child' => '分配权限'
        ];
    }

    public function createRole($name, $description)
    {
        $auth = Yii::$app->authManager;
        $role = $auth->createRole($name);
        $role->description = $description;
        $status = $auth->add($role);
        return $status;
    }

    public function createPermission($name, $description)
    {
        $auth = Yii::$app->authManager;
        $createPost = $auth->createPermission($name);
        $createPost->description = $description;
        $name = $auth->add($createPost);
    }
}
